﻿using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfBill.Data;

namespace WpfBill.GUI
{
    /// <summary>
    /// WinProject.xaml 的交互逻辑
    /// </summary>
    public partial class WinProject : WindowX
    {
        /// <summary>
        /// 单位信息集合
        /// </summary>
        private IList<Enterprise> enterprises = null;
        public WinProject()
        {
            InitializeComponent();
            this.Loaded += WinProject_Loaded;
        }

        private void WinProject_Loaded(object sender, RoutedEventArgs e)
        {
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                enterprises = Enterprise.FindAll();
                // var names = enterprises.Select(p => p.Name).ToArray();
                cmbA.Items.Clear();
                cmbB.Items.Clear();
                foreach (var item in enterprises)
                {
                    cmbA.Items.Add(new ComboBoxItem() { Content = item.Name });
                    cmbB.Items.Add(new ComboBoxItem() { Content = item.Name });
                }
                //cmbA.ItemsSource = names;
                //cmbB.ItemsSource = names;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        /// <summary>
        /// 重置界面
        /// </summary>
        private void ResetUI()
        {
            txtName.Text = "";
            cmbA.SelectedIndex = -1;
            cmbB.SelectedIndex = -1;
            txtAccountA.Text = "";
            txtPersonA.Text = "";
            txtPhoneA.Text = "";
            txtAccountB.Text = "";
            txtPersonB.Text = "";
            txtPhoneB.Text = "";
        }

        /// <summary>
        /// 通过名称获取企业信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private Enterprise FindCompanyByName(string name)
        {
            try
            {
                var item = enterprises.Find(p => p.Name == name);
                return item;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return null;
        }

        private void CmbA_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Project pro = new Project();
                pro.Name = txtName.Text.Trim();
                pro.EnterpriseA = cmbA.Text.Trim();
                pro.EnterpriseB = cmbB.Text.Trim();
                pro.AccountA = txtAccountA.Text.Trim();
                pro.AccountB = txtAccountB.Text.Trim();
                pro.PersonA = txtPersonA.Text.Trim();
                pro.PersonB = txtPersonB.Text.Trim();
                pro.PhoneA = txtPhoneA.Text.Trim();
                pro.PhoneB = txtPhoneB.Text.Trim();

                if (string.IsNullOrEmpty(pro.Name))
                {
                    MessageBox.Show("名称不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(pro.EnterpriseA) || string.IsNullOrEmpty(pro.EnterpriseB))
                {
                    MessageBox.Show("单位名称不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(pro.PersonA) || string.IsNullOrEmpty(pro.PersonB))
                {
                    MessageBox.Show("单位负责人不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(pro.AccountA) || string.IsNullOrEmpty(pro.AccountB))
                {
                    MessageBox.Show("单位账户不能为空");
                    return;
                }

                var company = FindCompanyByName(pro.EnterpriseA);
                if (company == null)
                {
                    Enterprise enterprise = new Enterprise() { Name = pro.EnterpriseA, Account = pro.AccountA, Person = pro.PersonA, Phone = pro.PhoneA };
                    enterprise.Insert();
                }
                var companyb = FindCompanyByName(pro.EnterpriseB);
                if (companyb == null)
                {
                    Enterprise enterprise = new Enterprise() { Name = pro.EnterpriseB, Account = pro.AccountB, Person = pro.PersonB, Phone = pro.PhoneB };
                    enterprise.Insert();
                }
                pro.Insert();
                if (chkContinue.IsChecked == true)
                {
                    ResetUI();
                }
                else
                {
                    this.DialogResult = true;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CmbB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cmbB.SelectedItem == null)
                    return;
                var company = FindCompanyByName((cmbB.SelectedItem as ComboBoxItem).Content.ToString().Trim());
                if (company != null)
                {
                    cmbB.Text = company.Name;
                    txtAccountB.Text = company.Account;
                    txtPersonB.Text = company.Person;
                    txtPhoneB.Text = company.Phone;
                }
            }catch(Exception ex)
            {

            }
        }

        private void CmbA_SearchTextChanged(object sender, Panuon.UI.Silver.Core.SearchTextChangedEventArgs e)
        {
            try
            {
                ComboBox comboBox = sender as ComboBox;
                foreach (ComboBoxItem item in comboBox.Items)
                {
                    item.Visibility = item.Content.ToString().Contains(e.Text) ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void CmbA_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cmbA.SelectedItem == null)
                    return;
                var company = FindCompanyByName((cmbA.SelectedItem as ComboBoxItem).Content.ToString().Trim());
                if (company != null)
                {
                    cmbA.Text = company.Name;
                    txtAccountA.Text = company.Account;
                    txtPersonA.Text = company.Person;
                    txtPhoneA.Text = company.Phone;
                }
            }catch(Exception ex)
            {

            }
        }
    }
}
